//
//  HomeVC.m
//  sensen_ios
//
//  Created by pro on 16/1/30.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "HomeVC.h"
#import "ZKTextImgReplaceHandle.h"
// 控制器
#import "ShopVC.h" // 休闲购物
//#import "ForumVC.h" // 休闲论坛
#import "ForumHomeVC.h" // 休闲论坛
#import "DeviceListVC.h"//设备
#import "NewsTotalVC.h"//新闻资讯

// 视图
#import "CarouselView.h" // 轮播视图
#import "HomeSectionView.h" // 板块视图
#import "WebViewController.h"


#import "TermConditionView.h"

@interface HomeVC ()

@property (nonatomic, strong) UIScrollView *scrollView; // 滑动视图

//@property (nonatomic, strong) CarouselView *firstCarouselView; // 第一个轮播视图
@property (nonatomic, strong) CarouselView *secondCarouselView; // 第二个轮播视图

@property (nonatomic, strong) HomeSectionView *aqView; // AQ·鱼保姆
@property (nonatomic, strong) HomeSectionView *newsView; // 新闻·资讯
@property (nonatomic, strong) HomeSectionView *shopView; // 休闲购物
@property (nonatomic, strong) HomeSectionView *forumView; // 休闲论坛
@property (nonatomic, strong) HomeSectionView *sensenView; // 森森官网

@property (nonatomic, strong) TermConditionView *termConditionView;
@end

@implementation HomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"森森休闲";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self _createViews]; // 创建视图
    [self _getData];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_ORANGE_COLOR] forBarMetrics:UIBarMetricsDefault];

}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    self.termConditionView.hidden = YES;
}


#pragma mark - 获取数据
- (void)_getData {
/*
    // 获得
    NSDictionary *params = @{@"position" : @6045};
    [BoyeHttpClient post:@"BY_Picture_getURL" apiVerson:100 params:params success:^(AFHTTPRequestOperation *operation, id result) {
        // 轮播图不为空
        if (![result isKindOfClass:[NSNull class]]) {
            NSMutableArray *ary = [NSMutableArray array];
            for (NSDictionary *dic in result) {
                CarouselModel *model = [[CarouselModel alloc] initContentWithDic:dic];
                
                [ary addObject:model];
            }
            
            // 设置轮播图
            _firstCarouselView.modelAry = ary;
        }
        // 轮播图为空
        else {
            // 轮播图为空，则传入一个元素的数组，让轮播图加载默认图片
            _firstCarouselView.modelAry = nil;
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 失败
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",error.domain] withDuration:2];
    }];
*/
    // 获得轮播图
//    params = @{@"position" : @6046};
    NSDictionary *params = @{@"position" : @6045};
    [BoyeHttpClient post:@"BY_Picture_getURL" apiVerson:100 params:params success:^(AFHTTPRequestOperation *operation, id result) {
        
        // 轮播图不为空
        if (![result isKindOfClass:[NSNull class]]) {
            NSMutableArray *ary = [NSMutableArray array];
            for (NSDictionary *dic in result) {
                CarouselModel *model = [[CarouselModel alloc] initContentWithDic:dic];
                
                [ary addObject:model];
            }
            
            // 设置轮播图
            _secondCarouselView.modelAry = ary;
        }
        // 轮播图为空
        else {
            // 轮播图为空，则传入一个元素的数组，让轮播图加载默认图片
            _secondCarouselView.modelAry = nil;
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 失败
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",error.domain] withDuration:2];
    }];
    
    

}

#pragma mark - 创建视图
- (void)_createViews {
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    _scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    if ([self isIphone4]) {
        _scrollView.contentSize = CGSizeMake(0, _scrollView.height - 48 + (568 - 480));
    } else {
        _scrollView.scrollEnabled = NO;
    }
    
    [self _createCarouselView];
    [self _createBottomView];
}
// 创建上面的两个视图（可能是轮播视图）
- (void)_createCarouselView {
    
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = SCREEN_WIDTH;
//    CGFloat height = width * (240.0 / 750.0);
    CGFloat height = width/2;
//    CGFloat margin = 6; // 间隙
    
//    _firstCarouselView = [[CarouselView alloc] initWithFrame:CGRectMake(x, y, width, height) timerDuration:3 carouselAmount:3];
//    [_scrollView addSubview:_firstCarouselView];
    
//    y = _firstCarouselView.bottom + margin;
    _secondCarouselView = [[CarouselView alloc] initWithFrame:CGRectMake(x, y, width, height) timerDuration:4 carouselAmount:3];
    [_scrollView addSubview:_secondCarouselView];
    
}
// 创建下面的5个板块的视图
- (void)_createBottomView {
    
    UIColor *aqColor = [UIColor colorWithR:29 g:89 b:166];
    UIColor *newsColor = [UIColor colorWithR:53 g:157 b:50];
    UIColor *shopColor = [UIColor colorWithR:245 g:143 b:14];
    UIColor *forumColor = [UIColor colorWithR:118 g:126 b:254];
    UIColor *sensenColor = [UIColor colorWithR:242 g:68 b:98];
    
    // 为了适配iphne4（iphone上方一个滑动视图，使其可以滑动）
    CGFloat viewHeight = [self isIphone4] ? 568 : SCREEN_HEIGHT;
    
    CGFloat margin = 6; // 间隙
//   CGF loat leftHeight = (viewHeight - 64 - 49 - _firstCarouselView.height - _secondCarouselView.height - 5 * margin);
    CGFloat leftHeight = (viewHeight - 64 - 49  - _secondCarouselView.height - 5 * margin);
    CGFloat leftWidth = (SCREEN_WIDTH - 3 * margin);
    
    CGFloat x = margin;
    CGFloat y = _secondCarouselView.bottom + margin;
    CGFloat width = leftWidth * 0.65;
    CGFloat height = leftHeight * 0.4;
    
    // AQ鱼保姆
    _aqView = [[HomeSectionView alloc] initWithFrame:CGRectMake(x, y, width, height) type:ZKHomeSectionViewTypeV];
    _aqView.backgroundColor = aqColor;
    _aqView.title = @"AQ·鱼保姆";
    _aqView.imgName = @"AQ鱼保姆";
    [_scrollView addSubview:_aqView];
    UITapGestureRecognizer *equipTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(equipAction:)];
    [_aqView addGestureRecognizer:equipTap];

    
    // 新闻资讯
    y = _aqView.bottom + margin;
    height = leftHeight * 0.2;
    _newsView = [[HomeSectionView alloc] initWithFrame:CGRectMake(x, y, width, height) type:ZKHomeSectionViewTypeH];
    _newsView.backgroundColor = newsColor;
    _newsView.title = @"资讯·热读";
    _newsView.imgName = @"新闻资讯";
    UITapGestureRecognizer *newTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(newAction:)];
    [_newsView addGestureRecognizer:newTap];
    [_scrollView addSubview:_newsView];
    
    // 休闲购物
    y = _newsView.bottom + margin;
    height = leftHeight * 0.4;
    _shopView = [[HomeSectionView alloc] initWithFrame:CGRectMake(x, y, width, height) type:ZKHomeSectionViewTypeV];
    _shopView.backgroundColor = shopColor;
    _shopView.title = @"休闲购物";
    _shopView.imgName = @"休闲购物";
    [_scrollView addSubview:_shopView];
    UITapGestureRecognizer *shopTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shopAction:)];
    [_shopView addGestureRecognizer:shopTap];
    
    // 休闲论坛
    x = _aqView.right + margin;
    y = _aqView.top;
    width = leftWidth - _aqView.width;
    height = (leftHeight + margin) * 0.5; // 右侧只有两个三个间隙
    _forumView = [[HomeSectionView alloc] initWithFrame:CGRectMake(x, y, width, height) type:ZKHomeSectionViewTypeV];
    _forumView.backgroundColor = forumColor;
    _forumView.title = @"休闲论坛";
    _forumView.imgName = @"休闲论坛";
    [_scrollView addSubview:_forumView];
    UITapGestureRecognizer *forumTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(forumAction:)];
    [_forumView addGestureRecognizer:forumTap];
    
    // 森森官网
    y = _forumView.bottom + margin;
    _sensenView = [[HomeSectionView alloc] initWithFrame:CGRectMake(x, y, width, height) type:ZKHomeSectionViewTypeV];
    _sensenView.backgroundColor = sensenColor;
    _sensenView.title = @"森森官网";
    _sensenView.imgName = @"森森官网";
    [_scrollView addSubview:_sensenView];
    UITapGestureRecognizer *sensenTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sensenAction:)];
    [_sensenView addGestureRecognizer:sensenTap];

    
}


#pragma mark - 点击事件
- (void)sensenAction:(UITapGestureRecognizer *)tap {
    WebViewController *vc = [[WebViewController alloc] initWithUrlString:@"http://m.sunsuncn.com"];
    vc.titleString = @"森森官网";
    vc.color = rgba(12, 91, 158, 1);
    [self.navigationController pushViewController:vc animated:YES];
}
// 点击商城
- (void)shopAction:(UITapGestureRecognizer *)tap {
    ShopVC *vc = [[ShopVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
// 点击论坛
- (void)forumAction:(UITapGestureRecognizer *)tap {
    ForumHomeVC *vc = [[ForumHomeVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
//点击进入新闻
- (void)newAction:(UITapGestureRecognizer *)tap {
    NewsTotalVC *vc = [[NewsTotalVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

// 点击设备
- (void)equipAction:(UITapGestureRecognizer *)tap {
    
    UserModel *user = [CommonCache getUserInfo];
    if (user.uid) {
        
        self.termConditionView = [[TermConditionView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
        [self.scrollView addSubview:self.termConditionView];
        self.termConditionView.hidden = YES;
        
        [self.termConditionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.scrollView.mas_centerX);
            make.centerY.equalTo(self.scrollView.mas_centerY).offset(-20);
            make.size.mas_equalTo(CGSizeMake(300, 300));
        }];
        
        
        
        
        NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
        BOOL result = [[userDefault objectForKey:@"isTermCodition"] boolValue];

        if (!result) {
            self.termConditionView.hidden = NO;
        }else{
            self.termConditionView.hidden = YES;
            DeviceListVC *vc = (DeviceListVC *)[BoyeTool getVCWithSbIden:@"AQ" identify:@"AQ"];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        [self.termConditionView.segmentedControl addTarget:self action:@selector(segmentControlAction:) forControlEvents:UIControlEventValueChanged];
        
    }else{
            
        LoginVC *vc = [[LoginVC alloc] init];
        BaseNavVC *naviVc = [[BaseNavVC alloc] initWithRootViewController:vc];
        [self presentViewController:naviVc animated:YES completion:nil];
    }
}

- (void)segmentControlAction:(UISegmentedControl *)segmentedControl{
    NSLog(@"点击了  %ld", segmentedControl.selectedSegmentIndex);
    
    
    
    
     if (segmentedControl.selectedSegmentIndex == 0) {
        self.termConditionView.hidden = YES;
     }else if (segmentedControl.selectedSegmentIndex == 1){
        self.termConditionView.hidden = YES;
        
         if (self.termConditionView.selectButton.selected) {
            NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults  setObject:[NSNumber numberWithBool:YES] forKey:@"isTermCodition"];
            [userDefaults synchronize];
         }
        DeviceListVC *vc = (DeviceListVC *)[BoyeTool getVCWithSbIden:@"AQ" identify:@"AQ"];
        [self.navigationController pushViewController:vc animated:YES];

    }
    

}




#pragma mark - 其他
// 适配iphone4
- (BOOL)isIphone4 {
    if (SCREEN_HEIGHT == 480) {
        return YES;
    } else {
        return NO;
    }
}
@end
